package com.chance.cc.crawler.development.command.schedule.xhs;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.development.command.meta.oauth.CrawlerOauthCommand;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.io.IOUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bx
 * @date 2020/12/15 0015 13:26
 */
@Data
@AllArgsConstructor
public class XHSAuthorInfoImport {

    private String keyword;

    private String category;

    public static final String headerReferer = "referer";
    public static final String headerAuthorization = "authorization";
    public static final String headerDeviceFingerprint = "device-fingerprint";
    public static final String authorOpenId = "openId";
    public static final String authorSid = "sid";
    public static final String authorFlag = "flag"; //手机标识
    public static final String authorType = "type"; //类型

//    private String businessTag;

    public static void main(String[] args) throws IOException {


        List<String> kwLines = IOUtils.readLines(new FileInputStream("D:\\chance\\gitspace\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\schedule\\xhs\\xhs_author"), Charset.forName("UTF-8"));

        List<Map<String,String>> allUserOauthInfos = new ArrayList<>();
        for (String kwLine : kwLines) {
            if (kwLine.startsWith("#")){
                continue;
            }
            String[] oauthInfos = kwLine.split("\t");
            Map<String,String> authMap = new HashMap<>();
            authMap.put(headerAuthorization,oauthInfos[0]);
            authMap.put(headerDeviceFingerprint,oauthInfos[1]);
            authMap.put(headerReferer,oauthInfos[2]);
            authMap.put(authorOpenId,oauthInfos[3]);
            authMap.put(authorSid,oauthInfos[4]);
            authMap.put(authorFlag,oauthInfos[5]);
            authMap.put(authorType,oauthInfos[6]);
            Map<String,String> userOauthInfoMap = new HashMap<>();
            userOauthInfoMap.put("domain","xhs");
            userOauthInfoMap.put("oauthInfo",JSON.toJSONString(authMap));
            System.out.println(JSON.toJSONString(userOauthInfoMap));
            allUserOauthInfos.add(userOauthInfoMap);
        }

        CrawlerOauthCommand crawlerOauthCommand = new CrawlerOauthCommand("192.168.1.215",9599);


        HttpPage httpPage = crawlerOauthCommand.addOrUpdateUserOAuthInfo(allUserOauthInfos);

        System.out.println(httpPage.getRawText());

    }
}
